package com.wcc.mpmultidb.service.impl;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.wcc.mpmultidb.entity.User;
import com.wcc.mpmultidb.entity.UserPg;
import com.wcc.mpmultidb.service.MasterUserService;
import com.wcc.mpmultidb.service.SlaveUserService;
import com.wcc.mpmultidb.service.TranService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author wuch
 * @version 1.0
 * @date 2021/4/23 18:08
 */
@Service
public class TranServiceImpl implements TranService {
    @Resource
    private MasterUserService masterUserService;

    @Resource
    private SlaveUserService slaveUserService;

    @Override
   // @Transactional(rollbackFor = RuntimeException.class) //事务控制
   //  @DSTransactional
    public boolean tranSave(User user) {
        try {
            UserPg userPg = new UserPg();
            BeanUtils.copyProperties(user,userPg);
//            boolean save = slaveUserService.save(userPg);
            this.save(user);
            slaveUserService.saveUser(userPg);
            return true;
        } catch (Exception e) {
            throw new RuntimeException("失败",e);
            // return false;
        }
    }

    // @Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.REQUIRED)
   // @DSTransactional
    public void save(User user){
            masterUserService.save(user);
            int i = 1/0;
    }


}
